<?php


class RegModel{
	private $m_db;
	
	//Sätter mysqli objektet
	public function __construct(Mysqli $db){
		$this->m_db = $db;
	}
	//Kontrollerar om användarnamn finns i databas
	public function UsernameExist($username){
		$stmt = $this->m_db->prepare("SELECT * FROM user WHERE `username` = ?;");
		$stmt->bind_param("s", $username);  
		$stmt->execute();
		$stmt->store_result();
		
		if($stmt->num_rows == 0){
			return true;	
		}
		return false;			
	}
	//Registrerar användaruppgifter i databas
	public function DoRegister($username, $password){
		$stmt = $this->m_db->prepare("INSERT INTO user (`username`, `password`) VALUES (?, ?);");
		$stmt->bind_param("ss", $username, $password); 
		If($stmt->execute() == false){
			return false;
		}
		$stmt->close();
		
		return true;
	}
	//Tar bort användaruppgifter från databas
	public function RemoveUser($username){
		$stmt = $this->m_db->prepare("DELETE FROM user WHERE `username`=?;");
		$stmt->bind_param("s", $username); 
		If($stmt->execute() == false){
			return false;
		}
		$stmt->close();
		
		return true;		
	}
	
}

